*********************************************************
*** *** Get all outpatient claims from opr files

cd /disk/aging/medicare/data/harm/100pct/op

foreach YEAR of numlist 2002/2016{
	disp `YEAR'
 	use hcpcs_cd rev_dt rprvdpmt file_year clm_id using `YEAR'/opr`YEAR'.dta
	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op/op`YEAR'.dta, replace
* }

* *** Drop bad obs; tag botox and split for ICD9 tagging
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op
* foreach YEAR of numlist 2016/2016{
* 	use op`YEAR'.dta, clear 
* 	drop if mi(hcpcs_cd)|mi(rev_dt)
* 	replace hcpcs_cd = "botox" if inlist(hcpcs_cd, "J0585", "64612", "64613", "64614"  "64640", "64650", "67345")
* 	*save op`YEAR'.dta, replace 

* 	keep if hcpcs_cd == "botox"
* 	save op`YEAR'_botox.dta, replace 
* }

********************************************************************************************
* *** Panelize all the non-botox claims
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op
* foreach YEAR of numlist 2002/2016{
* 	use op`YEAR'.dta, clear 
* 	drop if hcpcs_cd == "botox"
* 	gen dt = mofd(rev_dt)
* 	drop clm_id file_year
* 	collapse (sum) rprvdpmt, by(hcpcs_cd dt)
* 	save op`YEAR'_panel.dta, replace 
* }

* **** Merge into 1 file
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op
* use op2002_panel.dta, clear
* foreach YEAR of numlist 2003/2016{
* 	append using op`YEAR'_panel.dta
* }
* *** Drop after change to ICD9 diagnosis codes
* drop if dt >= 669

* *** Make sure 1 obs per period after appending
* *** In case some claims spillover between years
* collapse(sum) rprvdpmt, by(hcpcs_cd dt)

* *** Save as complete panel
* save op_hcpcs_panel.dta 


* ************************************************************
* *** Tag the botox with ICD codes 
* *** Clm ID is repeated for claims split more than one line item
* ** but should map to 1 diagnosis code

* **** 2002-2005: can merge on principal diagnosis code
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op

* foreach YEAR of numlist 2002/2005{
*  	use op`YEAR'_botox.dta
*  	disp `YEAR'
*  	disp "merging"
*  	merge m:1 clm_id using /disk/aging/medicare/data/harm/100pct/op/`YEAR'/opc`YEAR'.dta, keep(master match) keepusing(prncpal_dgns_cd)
*  	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op`YEAR'_botox_dgns.dta, replace
*  }

* *** 06-07: merge on OPC files with icd_dgns_cd1
* foreach YEAR of numlist 2006/2007{
*  	use op`YEAR'_botox.dta
*  	disp `YEAR'
*  	disp "merging"
*  	merge m:1 clm_id using /disk/aging/medicare/data/harm/100pct/op/`YEAR'/opc`YEAR'.dta, keep(master match) keepusing(icd_dgns_cd1)
*  	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op/op`YEAR'_botox_dgns.dta, replace
*  }


* **** 2008: not working because clm_id not unique in opc file
* *** There are a few weird stray ones, just going to remove them manually
* cd /disk/aging/medicare/data/harm/100pct/op 
* use 2008/opc2008.dta
* keep clm_id icd_dgns_cd1
* duplicates drop 
* drop if mi(clm_id)

* *** To find strays: duplicates tag clm_id, gen(dup)
* drop if mi(icd_dgns_cd1)
* drop if inlist(clm_id, "0.0", "W")

* *** Assert no more dupes
* duplicates tag clm_id, gen(dup)
* summarize dup 
* assert(r(min) == r(max))
* save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op/opc2008_clean.dta, replace 

*** 08: merge on local cleaned up opc file
* foreach YEAR of numlist 2008/2008{
* 	cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/
*  	use op`YEAR'_botox.dta
*  	disp `YEAR'
*  	disp "merging"
*  	merge m:1 clm_id using opc`YEAR'_clean.dta, keep(master match) keepusing(icd_dgns_cd1)
*  	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op/op`YEAR'_botox_dgns.dta, replace
*  }


* *** 2009: use icd_dgns_cd1
* foreach YEAR of numlist 2009/2009{
*  	use op`YEAR'_botox.dta
*  	disp `YEAR'
*  	disp "merging"
*  	merge m:1 clm_id using /disk/aging/medicare/data/harm/100pct/op/`YEAR'/opc`YEAR'.dta, keep(master match) keepusing(icd_dgns_cd1)
*  	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op/op`YEAR'_botox_dgns.dta, replace
*  }


*** 2010- 16: now named prncpal_dgns_cd

* foreach YEAR of numlist 2010/2016{
*  	use op`YEAR'_botox.dta
*  	disp `YEAR'
*  	disp "merging"
*  	merge m:1 clm_id using /disk/aging/medicare/data/harm/100pct/op/`YEAR'/opc`YEAR'.dta, keep(master match) keepusing(prncpal_dgns_cd)
*  	save /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op/op`YEAR'_botox_dgns.dta, replace
*  }


* ************************************************************
* * *** Filter and panelize botox
* cd /disk/agedisk3/medicare.work/poterba-DUA52260/jetson-dua52260/botox/synth/100pct/raw/op
* use op2002_botox_dgns.dta
* foreach YEAR of numlist 2003/2016{
* 	append using op`YEAR'_botox_dgns.dta
* }
* count
* 	*965k


* ** Clean diagnosis code into 1 column
* gen diagnosis = prncpal_dgns_cd
* replace diagnosis = icd_dgns_cd1 if mi(diagnosis)

* ** Clean date
* gen dt = mofd(rev_dt)

* *** Move to ICD10: October 1, 2015
* disp mofd(mdy(10, 1, 2015))
* 	*669 
* drop if dt >= 669

* ** Tag diagnosis codes mentioned in settlement 
* replace hcpcs_cd = "botox_treated" if diagnosis == "72885"| diagnosis == "3518"| diagnosis == "33383"| diagnosis == "7235"| diagnosis == "78830"| diagnosis == "78831"| diagnosis == "78832"| diagnosis == "78833"| diagnosis == "78834"| diagnosis == "59982"
* replace hcpcs_cd = "botox_untreated" if hcpcs_cd == "botox"
 
* collapse (sum) rprvdpmt, by(hcpcs_cd dt)

* save op_botox_panel.dta, replace

  


